using Microsoft.EntityFrameworkCore;
using Process.Api;
using Process.BLL;
using Process.Infastructure;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddJsonOptions(options =>
{
    // 如有循环引用，直接忽略，避免 Swagger/序列化报错
    options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x=>x.IncludeXmlComments(AppContext.BaseDirectory+ "Process.Api.xml",true));

//连接数据库
builder.Services.AddDbContext<ProcessDbContext>(x=>x.UseSqlServer(builder.Configuration.GetConnectionString("constr")));

//EF
builder.Services.AddScoped(typeof(IProcessRepository<>), typeof(ProcessRepository<>));

//中介者
builder.Services.AddMediatR(x => x.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()));

//AutoMapper
builder.Services.AddAutoMapper(typeof(AutoMapperFile));


var app = builder.Build();


// Configure the HTTP request pipeline.

    app.UseSwagger();
    app.UseSwaggerUI();

//跨域
app.UseCors(x=>x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

app.UseAuthorization();

app.MapControllers();

app.Run();
